package com.elvin.platform.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.elvin.platform.beans.Dictionary;
import com.elvin.platform.mapper.DictionaryMapper;
import com.elvin.platform.service.DictionaryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class DictionaryServiceImpl extends ServiceImpl<DictionaryMapper, Dictionary> implements DictionaryService {
	@Resource
	private DictionaryMapper mapper;

	@Override
	public void insertDictionary(Dictionary entity) {
		//新增的时候需要重新编写组织机构编
		mapper.insert(entity);
		//获取父id
		String parentIdStr = mapper.selectSelfAndParentId(entity.getId());
		String[] split = parentIdStr.split(",");
		StringBuffer stringBuffer = new StringBuffer();
		for (int i = split.length-1; i>=0; i--) {
			stringBuffer.append(split[i]);
			if(i !=0){
				stringBuffer.append("-");
			}
		}
		String code = stringBuffer.toString();
		entity.setParam1(code);
		mapper.updateById(entity);

	}

  @Override
  public List<Dictionary> selectDictionaryList(Integer id) {
    //获取父id
    String ids = mapper.selectSelfAndParentId(id);
    return mapper.selectDictionaryList(ids);
  }
}
